//
//  LXUserAccountTools.swift
//  weibo
//
//  Created by lixin on 16/4/19.
//  Copyright © 2016年 lixin. All rights reserved.
//

import UIKit

class LXUserAccountTools: NSObject {
    
    static let sharedTools:LXUserAccountTools = LXUserAccountTools()
    
    var userAccount: LXUserAccount?
    
    var isLogin: Bool{
    
        if accessToken != nil {
            
            return true
        
        }else{
        
            return false
        }
    }
    
    var accessToken: String?{
    
        if userAccount?.access_token != nil {
            
            if !isExpires {
                
                return userAccount?.access_token
           
            }else{
            
                return nil
            }
            
        }else{
        
            return nil
        }
    }
    
    var isExpires: Bool{
    
        if userAccount?.expires_date?.compare(NSDate()) == NSComparisonResult.OrderedDescending {
            
            return false
        
        }else{
        
            return true
        }
    }
    
    //路径
    let path = (NSSearchPathForDirectoriesInDomains(.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("userAccount.archiver")
    
    override init() {
        
        super.init()
        
        userAccount = getUserAccount()
    }

    //保存数据
    func saveUserAccount(account: LXUserAccount){
    
        userAccount = account
        
        NSKeyedArchiver.archiveRootObject(account, toFile: path)
    }
    
    //读取数据
    func getUserAccount() -> LXUserAccount?{
    
        return NSKeyedUnarchiver.unarchiveObjectWithFile(path) as? LXUserAccount
    }
}
